package com.tql.mall.merchant.controller.merchant;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tql.mall.common.result.R;
import com.tql.mall.merchant.service.ShopInfoService;
import com.tql.mall.model.merchant.ShopInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(description = "商家店铺相关接口")
@RestController
@RequestMapping("/merchant/shop")
public class ShopController {

    @Autowired
    private ShopInfoService shopInfoService;

    /**
     * 根据商家id查询其下的所有店铺信息
     * @param id : 是商家id，对应着 mer_id
     * @return
     */
    @ApiOperation(value = "根据商家id查询其下的所有店铺信息")
    @GetMapping("/getShop/{id}")
    public R<List<ShopInfo>> checkStock(@PathVariable Long id){
        QueryWrapper<ShopInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mer_id",id);
        List<ShopInfo> list = shopInfoService.list(queryWrapper);
        return R.ok(list);
    }

    /**
     * 修改店铺信息
     * @param shopInfo
     * @return
     */
    @ApiOperation(value = "修改店铺信息")
    @PutMapping("/update")
    public R<Void> updateShop(ShopInfo shopInfo){
        Boolean flag = shopInfoService.updateShop(shopInfo);
        if (flag) return R.ok();
        return R.fail();
    }

    @ApiOperation(value = "修改店铺锁定状态")
    @PutMapping("/updateStatus/{id}/{status}")
    public R<Void> updateShop(@PathVariable Long id,@PathVariable Integer status){
        shopInfoService.updateStatus(id,status);
        return R.ok();
    }

    @ApiOperation(value = "根据id获取店铺信息")
    @GetMapping("/getShopInfo/{id}")
    public R<ShopInfo> getShopInfo(@PathVariable Long id){
        return R.ok(shopInfoService.getById(id));
    }


    @ApiOperation(value = "提交修改店铺信息的表单")
    @PostMapping ("/saveShopInfo")
    public R<Void> saveOrUpdate(@RequestBody ShopInfo shopInfo){
        boolean flag = shopInfoService.saveOrUpdate(shopInfo);
        if (flag) return R.ok();
        return R.fail();
    }


}
